package yanglei.day0109.lottery.dao.impl;


import yanglei.day0109.lottery.dao.UserDao;
import yanglei.day0109.lottery.po.User;
import yanglei.day0109.lottery.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: YangLei
 * @CreateTime: 2023-01-07  16:21
 * @Description: TODO
 */
public class UserDaoImpl extends BaseDao implements UserDao {

    @Override
    public List<User> queryUserByType(Map<String, Object> map) {
        List<User> users = new ArrayList<>();
        User user = null;
        StringBuffer sql = new StringBuffer();
        sql.append("select id,user_name,mark_no,status,phone,to_char(open_date, 'yyyy-mm-dd') open_date from t_user");
        sql.append(" where 1 = 1");
        if (map != null && map.size() > 0) {
            Object markNo = map.get("markNo");
            if (markNo != null) {
                sql.append(" and mark_no like ? ");
                rs = super.executeQuery(sql.toString(), new Object[]{ "%" + markNo + "%"});
            }
        } else {
            //查询所有
            //调用公共的查询方法
            rs = super.executeQuery(sql.toString());
        }
        try {
            while (rs.next()) {
                user = new User(rs.getInt("id"), rs.getString("user_name"),
                        rs.getString("mark_no"), rs.getInt("status"),
                        rs.getString("phone"), rs.getString("open_date"));
                users.add(user);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return users;
    }

    @Override
    public int insertUser(User user) {
        int addRes = 0;
        StringBuffer sql = new StringBuffer();
        sql.append(" insert into t_user (id,user_name,mark_no,status,phone,open_date) values(lottery_user_id.nextval, ");
        sql.append(" ?,?,1,?,to_date(?,'yyyy-mm-dd')) ");
        addRes = super.executeUpdate(sql.toString(),new Object[]{user.getUserName(),user.getMarkNo(),user.getPhone(),
        user.getOpenDate()});
        return addRes;
    }

}
